package xyz.bali16.module.netdisk.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import xyz.bali16.module.netdisk.entity.FileStorageStatistics;
import xyz.bali16.module.netdisk.entity.FileUser;
import xyz.bali16.module.netdisk.entity.UploadFile;
import xyz.bali16.module.netdisk.mapper.UploadFileMapper;
import xyz.bali16.module.netdisk.model.file.UploadFileAddParam;
import xyz.bali16.module.netdisk.model.file.UploadFilePageParam;
import xyz.bali16.module.netdisk.model.file.UploadFileUpdateParam;
import xyz.bali16.module.netdisk.model.fileuser.FileUserPageParam;
import xyz.bali16.module.netdisk.service.UploadFileService;
import xyz.bali16.module.netdisk.utils.FtpUtil;


import java.util.List;
import java.util.concurrent.TimeUnit;


@Service("uploadFileService")
public class UploadFileServiceImpl extends ServiceImpl<UploadFileMapper, UploadFile> implements UploadFileService {

    @Async
    @Override
    public void asyncRemoveById(String fileId) {
        try {
            TimeUnit.HOURS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        UploadFile uploadFile = baseMapper.selectById(fileId);
        if (FtpUtil.deleteFile("/" + uploadFile.getPath(), uploadFile.getName())) {
            FtpUtil.deleteFileFolder("/" + uploadFile.getPath());
            baseMapper.deleteById(fileId);
        }
    }



    @Override
    public IPage<UploadFile> getPage(UploadFilePageParam param) {
        IPage<?> page = new Page<>(param.getCurrent(), param.getSize());
        return this.baseMapper.getPage(page, param.getId(),param.getUUID(),param.getFileStorageId(),param.getName(),param.getType(),param.getSuffix(),param.getState());
    }



    @Override
    public Integer deleteByParentFolderId(String partentFolderId) {
        return baseMapper.deleteByParentFolderId(partentFolderId);
    }

    @Override
    public List<UploadFile> getRootFilesByFileStorageId(String fileStorageId) {
        return baseMapper.getRootFilesByFileStorageId(fileStorageId);
    }

    @Override
    public List<UploadFile> getFilesByParentFolderId(String parentFolderId) {
        return baseMapper.getFilesByParentFolderId(parentFolderId);
    }

    @Override
    public List<UploadFile> getFilesByType(String fileStorageId, Integer type) {
        return baseMapper.getFilesByType(fileStorageId, type);
    }

    @Override
    public FileStorageStatistics getCountStatistics(String fileStorageId) {
        return baseMapper.getCountStatistics(fileStorageId);
    }
//
//    @Override
//    public Integer addFileByFileStorageId(UploadFile uploadFile) {
////        baseMapper.(fileUser.getFileStorageId(), byteSize);
//        return null;
//    }
}
